<template>
  <Modal
    v-model="form.visible"
    :width="1000"
    :mask-closable="false"
    :loading="loading"
    :title="form.data.id ? '修改工作人员' : '新增工作人员'"
    ok-text="提交"
    @on-ok="ok">
    <Row class="mt30">
      <Col span="16" offset="4">
        <Form ref="form" :model="form.data" :rules="ruleValidate" :label-width="110">
          <FormItem prop="headImage" label="头像">
            <upload-img v-if="form.visible" @on-change="onUploadImg" :defaultImageList="defaultImageList" maxLength="1"></upload-img>
          </FormItem>
          <FormItem prop="employeeName" label="姓名">
            <Input v-model="form.data.employeeName" placeholder="请输入" :maxlength="20"></Input>
          </FormItem>
          <FormItem prop="nickname" label="昵称">
            <Input v-model="form.data.nickname" placeholder="请输入" :maxlength="20"></Input>
          </FormItem>
          <FormItem prop="sex" label="性别">
            <Select v-model="form.data.sex" style="width:200px">
              <Option :value="'1'">男</Option>
              <Option :value="'0'">女</Option>
            </Select>
          </FormItem>
          <FormItem prop="isEnable" label="外网是否显示">
            <Select v-model="form.data.isEnable" style="width:200px">
              <Option :value="0">是</Option>
              <Option :value="1">否</Option>
            </Select>
          </FormItem>
          <FormItem prop="status" label="状态">
            <Select v-model="form.data.status" style="width:200px">
              <Option :value="0">在职</Option>
              <Option :value="1">离职</Option>
            </Select>
          </FormItem>
          <FormItem prop="postion" label="岗位">
            <Input v-model="form.data.postion" placeholder="请输入" :maxlength="20"></Input>
          </FormItem>
          <FormItem prop="servicePromise" label="服务承诺">
            <Input type="textarea" v-model="form.data.servicePromise" placeholder="请输入" :rows="4" :maxlength="200"></Input>
          </FormItem>
          <FormItem prop="introduce" label="介绍">
            <Input type="textarea" v-model="form.data.introduce" placeholder="请输入" :rows="4" :maxlength="300"></Input>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </Modal>
</template>

<script>
  import Vue from 'vue'
  import { mapGetters } from 'vuex'
  import uploadImg from '@/components/upload-img'
  import clubApi from '@/api/club'

  export default {
    name: 'update-modal',
    props: [
      'form'
    ],
    components: {
      uploadImg
    },
    data () {
      return {
        loading: true,
        ruleValidate: {
          headImage: [
            { required: true, message: '请上传头像', trigger: 'change' }
          ],
          employeeName: [
            { required: true, message: '请输入姓名', trigger: 'change' }
          ],
          nickname: [
            { required: true, message: '请输入昵称', trigger: 'change' }
          ],
          postion: [
            { required: true, message: '请输入岗位', trigger: 'change' }
          ]
        }
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'userInfo'
      })
    },
    watch: {
      'form.visible': 'onClose'
    },
    methods: {
      ok () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.submitForm()
          } else {
            this.$Message.error('表单验证失败，请检查输入项目!')
            this.form.visible = true
            this.loading = false
            setTimeout(() => {
              this.loading = true
            }, 100)
          }
        })
      },
      onClose (isVisible) {
        if (!isVisible) {
          this.$nextTick(() => {
            this.$refs.form.resetFields()
          })
        } else {
          this.defaultImageList = Vue.filter('splitImg')(this.form.data.headImage || '')
        }
      },
      onUploadImg (imgList) {
        this.form.data.headImage = imgList.filter((item) => {
          return item.name.length > 0
        }).map((item) => {
          if (item.response && item.response.responseData) {
            return item.response.responseData
          } else {
            return item.name
          }
        }).join(',')
        this.$refs.form.validateField('headImage')
      },
      async submitForm () {
        this.form.data.pkRclubInfo = this.userInfo.pkRclubInfo
        if (this.form.data.id) {
          const { body } = await clubApi.updateEmployee(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('修改成功')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
            }, 500)
          }
        } else {
          const { body } = await clubApi.createEmployee(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('创建成功')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
            }, 500)
          }
        }
        this.form.visible = false
        this.$emit('submit')
      }
    }
  }
</script>
